package com.tucy.logistics.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tucy.logistics.common.result.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * author: tcy
 * createDate: 2023/9/15
 * description:
 */
@Slf4j
@Component
public class HttpRequestUtil {

    public String postRequest(String url,Object data) throws Exception {

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setHeader("Accept-Encoding", "gzip,deflate");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(JSONUtil.toJsonStr("如果有请求对象，这里添加" + data), StandardCharsets.UTF_8));

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        String responseBody = EntityUtils.toString(responseEntity);

        if (ObjectUtil.isNotEmpty(responseBody)){
            return responseBody;
        }else {
            throw new ServiceException("调用失败");
        }
    }

    public String getRequest(String url) throws Exception {

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpPost = new HttpGet(url);

        httpPost.setHeader("Accept-Encoding", "gzip,deflate");
        httpPost.setHeader("Content-Type", "application/json");

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        String responseBody = EntityUtils.toString(responseEntity);

        if (ObjectUtil.isNotEmpty(responseBody)){
            return responseBody;
        }else {
            throw new ServiceException("调用失败");
        }
    }

    public String huToolGetRequest(String url) {

        HttpRequest httpRequest = HttpRequest.get(url);
        cn.hutool.http.HttpResponse response = httpRequest.execute();

        if (ObjectUtil.isNotEmpty(response.body())) {
            return response.body();
        } else {
            throw new ServiceException("调用失败");
        }
    }

    public String huToolPostRequest(String url) {

        HttpRequest httpRequest = HttpRequest.post(url);
        cn.hutool.http.HttpResponse response = httpRequest.execute();

        if (ObjectUtil.isNotEmpty(response.body())) {
            return response.body();
        } else {
            throw new ServiceException("调用失败");
        }
    }
}
